package com.kikyou.utils;

import org.csource.fastdfs.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.multipart.MultipartFile;


import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @ClassName FastDFSUtils 连接工具类
 * @Description T0D0 工具类
 * @Author ykq11
 * @Date 2022/2/22 19:24
 * @Version 1.0
 **/
public class FastDFSUtils {

    private static Logger logger = LoggerFactory.getLogger(FastDFSUtils.class);

    /**
     * Description : TODO  初始化客户端<br/>
     *   ClientGlobal.init读取配置文件，并初始化化对应的属性
     * ChangeLog : 1. 创建 (2022/2/22 19:29 [ykq11]);
     ** @param null
     * @return null
     **/
    static {
        try {
            String filePath = new ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath();
            ClientGlobal.init(filePath);
        } catch (Exception e) {
            logger.error("初始化FastFDS失败", e.getMessage());
        }
    }

    /**
     * Description : TODO 上传文件<br/>
     * ChangeLog : 1. 创建 (2022/2/22 19:51 [ykq11]);
     * * @param file
     *
     * @return java.lang.String[]
     **/
    public static String[] upload(MultipartFile file) {
        String name = file.getOriginalFilename();
        logger.info("文件名", name);
        StorageClient storageClient = null;
        String[] uploadResult = null;
        try {
            //获取storage 客户端
            storageClient = getStorageClient();
            //上传
            uploadResult = storageClient.upload_file(file.getBytes(), name.substring(name.lastIndexOf(".") + 1), null);
        } catch (Exception e) {
            logger.error("上传文件失败", e.getMessage());
        }
        if (null == uploadResult) {
            logger.error("上传失败", storageClient.getErrorCode());
        }
        return uploadResult;
    }
/**
 * Description : TODO  获取文件信息<br/>
 * ChangeLog : 1. 创建 (2022/2/22 19:54 [ykq11]);
 ** @param groupName
 * @param remoteFileName
 * @return org.csource.fastdfs.FileInfo
 **/
public static FileInfo getFileInfo(String groupName,String remoteFileName){
    StorageClient storageClient = null;
    try {
        storageClient=getStorageClient();
       return storageClient.get_file_info(groupName,remoteFileName);
    } catch (Exception e) {
        logger.error("文件信息获取失败", storageClient.getErrorCode());
    }
    return null;
}
/**
 * Description : TODO  下载文件<br/>
 * ChangeLog : 1. 创建 (2022/2/22 19:58 [ykq11]);
 ** @param groupName
 * @param remoteFileName
 * @return java.io.InputStream
 **/
public static InputStream downloadFile(String groupName,String remoteFileName){
    StorageClient storageClient = null;
    try {
        storageClient=getStorageClient();
        byte[] fileByte=storageClient.download_file(groupName,remoteFileName);
        return new ByteArrayInputStream(fileByte);
    } catch (Exception e) {
        logger.error("文件下载失败", storageClient.getErrorCode());
    }
    return null;
}
/**
 * Description : TODO  删除文件<br/>
 * ChangeLog : 1. 创建 (2022/2/22 20:02 [ykq11]);
 ** @param groupName
 * @param remoteFileName
 * @return void
 **/
public static void deleteFile(String groupName, String remoteFileName) {
    StorageClient storageClient = null;
    try {
        storageClient=getStorageClient();
        storageClient.delete_file(groupName, remoteFileName);
    } catch (Exception e) {
        logger.error("文件删除失败", storageClient.getErrorCode());
    }
}
    /**
     * Description : TODO  生成Storage 客户端<br/>
     * ChangeLog : 1. 创建 (2022/2/22 19:37 [ykq11]);
     * *
     *
     * @return org.csource.fastdfs.StorageClient
     **/

    private static StorageClient getStorageClient() throws IOException {
        TrackerServer trackerServer = getTrackerServer();
        return new StorageClient(trackerServer, null);
    }


    /**
     * Description : TODO  生成Tracker 服务器<br/>
     * ChangeLog : 1. 创建 (2022/2/22 19:34 [ykq11]);
     *
     * @return org.csource.fastdfs.TrackerServer
     **/
    private static TrackerServer getTrackerServer() throws IOException {
        TrackerClient trackerClient = new TrackerClient();
        return trackerClient.getTrackerServer();
    }
    /**
     * Description : TODO  获取文件路径<br/>
     * ChangeLog : 1. 创建 (2022/2/22 20:05 [ykq11]);
     * *
     * @return java.lang.String
     **/
    public static String getTrackerUrl(){
          TrackerClient trackerClient =new TrackerClient();
        TrackerServer trackerServer =null;
        StorageServer storeStorage =null;
        try {
             trackerServer = trackerClient.getTrackerServer();
             storeStorage = trackerClient.getStoreStorage(trackerServer);
        } catch (Exception e) {
            logger.error("文件路径获取失败", e.getMessage());
        }
        return "http://"+ storeStorage.getInetSocketAddress().getHostString()+":8888/";
    }
}
